package com.shujia

// 将共有的属性及方法抽象出来形成父类
class Person(id: String, name: String, age: Int) {
  val _id: String = id
  val _name: String = name
  val _age: Int = age

  def eat(): Unit = {
    println("人类会吃饭。")
  }

  def run(): Unit = {
    println("人类会跑。")
  }
}

class Stu(id: String, name: String, age: Int, sId: String) extends Person(id, name, age) {
  val _sId: String = sId

  override def eat(): Unit = { // 重写父类的方法
    println("学生吃了一碗饭。")
  }

  def learn(): Unit = {
    println("学生会学习")
  }

}

class Teacher(id: String, name: String, age: Int, tId: String) extends Person(id, name, age) {
  val _tId: String = tId

  override def eat(): Unit = { // 重写父类的方法
    println("老师也吃了一碗饭。")
  }

  def teach(): Unit = {
    println("老师会上课。")
  }

}


object Demo10ClassExtends {
  // 多态：父类引用指向子类对象
  def perEatTwice(per:Person):Unit = {
    per.eat()
    per.eat()
  }

  def main(args: Array[String]): Unit = {

    val zs: Person = new Person("01", "zs", 20)
    zs.eat()
    zs.run()

    val ls: Stu = new Stu("02", "ls", 22, "001")
    ls.eat()

    val zz: Teacher = new Teacher("03", "zz", 25, "001")
    zz.eat()

    perEatTwice(zs)
    perEatTwice(ls)
    perEatTwice(zz)

  }

}
